package ch.headshot.photomap.client.editor.view;

import ch.headshot.photomap.client.editor.control.CenterMapCommand;
import ch.headshot.photomap.client.editor.control.DeleteTrackCommand;
import ch.headshot.photomap.client.gpx.Track;
import ch.headshot.photomap.client.gpx.TrackSegment;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

public class TrackStructure extends HorizontalPanel {

	private final TrackButtonListener checkListener;
	private final CheckBox button;

	public TrackStructure(Track track) {
		button = new CheckBox();
		checkListener = new TrackButtonListener(track);
		button.addClickListener(checkListener);
		add(button);

		Label label = new Label(track.getName());
		label.addClickListener(new ContextMenuListener(track));
		add(label);

	}

	public void setChecked(boolean b) {
		if (button.isChecked()) {
			return;
		}
		checkListener.onClick(null);
		button.setChecked(true);
	}

	private final class TrackButtonListener implements ClickListener {

		private final Track track;
		boolean enabled = false;
		private Segment[] segments;

		public TrackButtonListener(Track track) {
			this.track = track;
		}

		public void onClick(Widget sender) {
			enabled = !enabled;
			if (enabled) {
				Map.getInstance().showSegments(getSegments());
				GpxStructure.getInstance().getSelectedTracks().add(track);
			} else {
				Map.getInstance().hideSegments(getSegments());
				GpxStructure.getInstance().getSelectedTracks().remove(track);
			}
		}

		private Segment[] getSegments() {
			if (segments == null) {
				segments = new Segment[track.getSegments().size()];
				int i = 0;
				for (TrackSegment segment : track.getSegments()) {
					segments[i] = new Segment(segment.getPoints());
					i++;
				}
			}

			return segments;

		}
	}

	private final class ContextMenuListener implements ClickListener {

		private final Track track;

		public ContextMenuListener(Track track) {
			this.track = track;
		}

		public void onClick(Widget sender) {
			final PopupPanel popupPanel = new PopupPanel(true);
			// TODO create a proper popup menu
			MenuBar menu = new MenuBar(true);

			MenuItem deleteItem = new MenuItem("delete", new CommandWrapper(popupPanel, new DeleteTrackCommand(track)));

			MenuItem centerItem = new MenuItem("center map", new CommandWrapper(popupPanel, new CenterMapCommand(track)));
			menu.addItem(deleteItem);
			menu.addItem(centerItem);
			popupPanel.add(menu);
			popupPanel.setPopupPosition(sender.getAbsoluteLeft(), sender.getAbsoluteTop());
			popupPanel.show();

		}

	}

	private static class CommandWrapper implements Command {

		private final Command command;
		private final PopupPanel popupPanel;

		public CommandWrapper(PopupPanel popupPanel, Command command) {
			assert command != null;
			assert popupPanel != null;
			this.popupPanel = popupPanel;
			this.command = command;
		}

		public void execute() {
			command.execute();
			popupPanel.hide();

		}

	}

}
